package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Crawlerknowledge;
import cn.edu.scau.cmi.ema.domain.Url;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("CrawlerknowledgeController")
public class CrawlerknowledgeController extends CmiEmaController {

	@RequestMapping("/listCrawlerknowledges")
	public ModelAndView listCrawlerknowledges(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = crawlerknowledgePaginationService.updatePagination(request, pagination);
		Set<Crawlerknowledge> crawlerknowledges = crawlerknowledgePaginationService
				.getPaginationCrawlerknowledges(pagination);

		mav.addObject("crawlerknowledges", crawlerknowledges);
		mav.addObject("entityName", "crawlerknowledge");
		mav.addObject("pagination", pagination);
		mav.setViewName("crawlerknowledge/listCrawlerknowledges");
		return mav;
	}

	public String listCrawlerknowledges() {
		return "redirect:/listCrawlerknowledges";
	}

	@RequestMapping("/viewCrawlerknowledge")
	public ModelAndView viewCrawlerknowledge(@RequestParam Integer crawlerknowledgeId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("crawlerknowledge", crawlerknowledgeDAO.findCrawlerknowledgeByPrimaryKey(crawlerknowledgeId));
		mav.setViewName("crawlerknowledge/viewCrawlerknowledge");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteCrawlerknowledges")
	public ModelAndView confirmDeleteCrawlerknowledges(
			@RequestParam(value = "deleteCrawlerknowledgesIds", required = true) Set<Integer> deleteCrawlerknowledgesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Crawlerknowledge> deleteCrawlerknowledges = crawlerknowledgeDAO
				.findCrawlerknowledgesByIds(deleteCrawlerknowledgesIds);
		Set<Crawlerknowledge> referencedCrawlerknowledges = crawlerknowledgeService
				.loadReferenceCrawlerknowledges(deleteCrawlerknowledges);
		boolean hasReferenced;
		if (referencedCrawlerknowledges.size() == 0) {
			mav.addObject("deleteEntitys", deleteCrawlerknowledges);
			hasReferenced = false;
		} else {
			deleteCrawlerknowledges.removeAll(referencedCrawlerknowledges);
			mav.addObject("referencedEntitys", referencedCrawlerknowledges);
			mav.addObject("deleteEntitys", deleteCrawlerknowledges);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Crawlerknowledge");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listCrawlerknowledges");
		return mav;
	}

	@RequestMapping("/deleteCrawlerknowledge")
	public String deleteCrawlerknowledge(@RequestParam Integer crawlerknowledgeId) {
		Crawlerknowledge crawlerknowledge = crawlerknowledgeDAO.findCrawlerknowledgeByPrimaryKey(crawlerknowledgeId);
		crawlerknowledgeService.deleteCrawlerknowledge(crawlerknowledge);
		return "forward:/listCrawlerknowledges";
	}

	@RequestMapping("/confirmDeleteCrawlerknowledge")
	public ModelAndView confirmDeleteCrawlerknowledge(@RequestParam Integer crawlerknowledgeId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("crawlerknowledge", crawlerknowledgeDAO.findCrawlerknowledgeByPrimaryKey(crawlerknowledgeId));

		mav.setViewName("crawlerknowledge/deleteCrawlerknowledge");

		return mav;
	}

	@RequestMapping("/deleteCrawlerknowledges")
	public String deleteCrawlerknowledges(
			@RequestParam(value = "deleteCrawlerknowledgesId", required = true) Set<Integer> deleteCrawlerknowledgesId) {
		crawlerknowledgeService.deleteCrawlerknowledges(deleteCrawlerknowledgesId);
		return "forward:/listCrawlerknowledges";
	}

	@RequestMapping("/editCrawlerknowledge")
	public ModelAndView editCrawlerknowledge(@RequestParam Integer crawlerknowledgeId) {

		ModelAndView mav = new ModelAndView();

		Crawlerknowledge crawlerknowledge = crawlerknowledgeDAO.findCrawlerknowledgeByPrimaryKey(crawlerknowledgeId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("crawlerknowledge", crawlerknowledge);

		// 把所有的引用crawlerknowledge对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerknowledgeService.loadCrawlerknowledgeObjectReferencedSet(crawlerknowledge));

		// 将编辑页面crawlerknowledge添加到mav中
		mav.setViewName("crawlerknowledge/editCrawlerknowledge");
		return mav;
	}

	@RequestMapping("/newCrawlerknowledge")
	public ModelAndView newCrawlerknowledge() {
		ModelAndView mav = new ModelAndView();
		Crawlerknowledge crawlerknowledge = new Crawlerknowledge();

		mav.addObject("crawlerknowledge", crawlerknowledge);

		// 把所有的引用crawlerknowledge对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerknowledgeService.loadCrawlerknowledgeObjectReferencedSet(crawlerknowledge));

		mav.addObject("newFlag", true);
		mav.setViewName("crawlerknowledge/editCrawlerknowledge");

		return mav;
	}

	@RequestMapping("/saveCrawlerknowledge")
	public ModelAndView saveCrawlerknowledge(@ModelAttribute Crawlerknowledge crawlerknowledge) {

		ModelAndView mav = new ModelAndView();

		Url urlTemp = crawlerknowledge.getUrl();
		Url url = urlTemp == null ? null : CmiHibernateProxyUtil.deproxy(urlDAO.findUrlById(urlTemp.getId()));

		Crawlerknowledge existingCrawlerknowledge = CmiHibernateProxyUtil
				.deproxy(crawlerknowledgeDAO.findCrawlerknowledgeByPhysicalPrimaryKey(url.getId()));

		crawlerknowledge.setUrl(url);
		// 新建记录
		if (crawlerknowledge.getId() == null) {
			if (crawlerknowledge.equals(existingCrawlerknowledge)) {

				mav.addAllObjects(crawlerknowledgeService.loadCrawlerknowledgeClassReferencedSet(crawlerknowledge));
				// mav = editContinue(mav, crawlerknowledge);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerknowledge/editCrawlerknowledge");
			} else {
				crawlerknowledgeService.saveCrawlerknowledge(crawlerknowledge);
				mav.setViewName("forward:/listCrawlerknowledges");
			}
		}
		// 编辑记录
		else {
			Crawlerknowledge originCrawlerknowledge = CmiHibernateProxyUtil
					.deproxy(crawlerknowledgeDAO.findCrawlerknowledgeByPrimaryKey(crawlerknowledge.getId()));
			if (!crawlerknowledge.equals(originCrawlerknowledge) && crawlerknowledge.equals(existingCrawlerknowledge)) {
				mav.addAllObjects(crawlerknowledgeService.loadCrawlerknowledgeClassReferencedSet(crawlerknowledge));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerknowledge/editCrawlerknowledge");
			} else {
				crawlerknowledgeService.saveCrawlerknowledge(crawlerknowledge);
				mav.setViewName("forward:/listCrawlerknowledges");
			}
		}
		return mav;
	}

	@RequestMapping("/crawlerknowledgeController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
